package hibernateDAOS;

import hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import beans.MedioPago;

public class MedioPagoDAO {
	
	private static MedioPagoDAO instancia = null;
	private static SessionFactory sf = null;

	public static MedioPagoDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new MedioPagoDAO();
		} 
		return instancia;
	}
	
	public void guardarMedioPago(MedioPago m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarMedioPago(MedioPago m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarMedioPago(MedioPago m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<MedioPago> obtenerMediosPagos(){
		Session session = sf.openSession();
		Query q = session.createQuery("from MedioPago");
		List<MedioPago> mediosPagos = q.list();
		session.close();
		return mediosPagos;
	}
}




